import axios from "axios";
import { getCookies } from "@/utils/auth";
import { toast } from "@/utils";

const request = axios.create({
  // 配置基础请求路径
  baseURL: "/api",
});

// 添加请求拦截器
request.interceptors.request.use(
  function (config) {
    //在头部添加token
    const token = getCookies();
    if (token) {
      config.headers["token"] = token;
    }
    return config;
  },
  function (error) {
    //对请求错误做些什么
    return Promise.reject(error);
  }
);

//添加响应拦截器
request.interceptors.response.use(
  function (response) {
    return response.data.data;
  },
  function (error) {
    toast(error.response.data.msg, "error");
    return Promise.reject(error);
  }
);

export default request;
